// mircoApp/cash-back/index/index.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "reward",
    totalData: {},
    list: [],
    isLoadMore: true,
    page: 1,
    totalPage: 1,
    url: "plugin.cash-back.frontend.modules.reward.controllers.records.index"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getdata();
  },
  clickAngle(event) {
    let index = event.target.dataset.index;
    this.setData({
      ['list[' + index + '].change']: !this.data.list[index].change
    });
  },
  onChange(event) {
    this.setData({
      active: event.detail.name
    });
    if (event.detail.name == "reward") this.data.url = "plugin.cash-back.frontend.modules.reward.controllers.records.index";
    else this.data.url = "plugin.cash-back.frontend.modules.times.controllers.records.index";
    this.getdata();
  },
  initdata() {
    this.setData({
      list: []
    });
    this.data.isLoadMore = false;
    this.data.page = 1;
    this.data.totalPage = 1;
  },
  getdata() {
    this.initdata();
    app._getNetWork({
      url:app.getNetAddresss(this.data.url),
      success:(resdata)=>{
        let data = resdata.data;
        if(data.result == 1){
          this.data.totalData = {
            surplus_count: data.data.surplus_count,
            grand_amount: data.data.grand_amount,
            grand_may_amount: data.data.grand_may_amount,
            avatar_image: data.data.avatar_image,
            nickname: data.data.nickname
          };
          this.setData({
            totalData: this.data.totalData,
            list: data.data.page_list.data,
          });
          this.data.isLoadMore = true;
          this.data.totalPage = data.data.page_list.last_page;
        }else{
          app.tips(data.msg);
        }
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page: this.data.page,
      };
      let urlStr = app.getNetAddresss(this.data.url);
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res?.data?.page_list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list : this.data.list.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})